const AJAX = getApp().ajax,
      $wx = getApp().$wx,
      getColor = getApp().globalData.getColor;

Page({
  data:{
    lazyLoadImg: getApp().globalData.lazyLoadImg,
    navList: [
      { name: '全部订单'},
      { name: '待评价' },
      { name: '退款' },
    ],    
    navItem: 0,
    orderList: {},
    orderListParams: { page: 1},
    orderStatus: {
      10: {
        order_text: '待付款',
        btn_text: '立即支付',
        fun: 'pay'
      },

      20: {
        order_text: '待接单',
        btn_text: '查看详情',
        fun: 'viewOrderDetail'
      },

      21: {
        order_text: '已接单',
        btn_text: '查看详情',
        fun: 'viewOrderDetail'
      },

      30: {
        order_text: '查看详情',
        btn_text: ' 配送中',
        fun: 'viewOrderDetail'
      },

      40: {
        order_text: '已完成',
        btn_text: '立即评价',
        fun: 'rating'
      },

      50: {
        order_text: '已取消',
        btn_text: '删除订单',
        fun: 'delOrder'
      },

      60: {
        order_text: '已退单',
        btn_text: '删除订单',
        fun: 'delOrder'
      }
    },
    isPayment: false,
    paymentType: {
      balanceMoney: 1,
      WeCahtMoney: 2
    }
  },

  onLoad(){
    $wx.setNavColor(getColor().val);
    this.setData({ getColor: getColor() });
    this.getMyMoney();
  },

  onShow(){
    this.getOrderList();    
  },
  
  tolower(){
    let _this = this, td = _this.data;

    if (td.orderListParams.page < td.orderList.last_page){
      ++ td.orderListParams.page;
      _this.getOrderList(true);
    }
  },

  onShareAppMessage(){

  },

  //获取导航项
  getNavItem(e){
    let _this = this, td = _this.data, 
        item = e.currentTarget.dataset.item;

    this.setData({
      navItem: item
    });

    switch(item){
      case 0:
        td.orderList.data = [];
        td.orderListParams = { page: 1};
        _this.getOrderList();
        break;

      case 1:
        td.orderList.data = [];
        td.orderListParams = { page: 1 ,is_comment: 1 };
        _this.getOrderList();
        break;
      
      case 2:
        td.orderList.data = [];
        td.orderListParams = { page: 1 , status: 60};
        _this.getOrderList();
        break;
    }
  },

  // 再次购买
  repurchase(e){
    let _this = this, order_id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/other-pages/submit-order/submit-order?order_id=' + order_id,
      success(){
        _this.clearData();
      }
    });
  },

  // 查看订单详情
  viewOrderDetail(e){
    let _this = this, order_id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/other-pages/order-detail/order-detail?order_id=' + order_id,
      success(){
        _this.clearData();
      }
    });
  },

  // 评论
  rating(e){
    let _this = this, is_comment = e.currentTarget.dataset.comment, 
        order_id = e.currentTarget.dataset.id;
        
    if (is_comment == 1){
      wx.navigateTo({
        url: '/pages/other-pages/order-rate/order-rate?order_id=' + order_id,
        success(){
          _this.clearData();
        }
      });
    }
  },

  // 支付
  pay(e){
    this.setData({
      isPayment: true,
      orderId: e.currentTarget.dataset.id
    });
  },

  paymentConfirm(e){
    let _this = this, td = _this.data,
        payment_info = e.detail.payment;
    
    if (!payment_info.type) {
      $wx.showToast('请选择支付方式');
    } else {
      _this.payOrder({
        pay_type: td.paymentType[payment_info.type],
        order_id: td.orderId
      });
    }
  },

  /**
   * 支付
   * @param pay_type
   * @param order_id
   * */
  payOrder(params = {}) {
    let _this = this;
    wx.showLoading({
      title: '支付中...'
    });

    AJAX.payOrder(params, (status, data, msg) => {
      wx.hideLoading();

      if (status) {
        if (params.pay_type == 1) {
          $wx.showToast(msg);

          setTimeout(callBack => {
            wx.navigateTo({
              url: '/pages/other-pages/order-detail/order-detail?order_id=' + data.order_id,
              success() {
                _this.setData({
                  isPayment: false
                });
                _this.clearData();
              }
            });
          }, 1500);
        } else if (params.pay_type == 2) {
          wx.requestPayment({
            timeStamp: data.timeStamp,
            nonceStr: data.nonceStr,
            package: data.package,
            signType: data.signType,
            paySign: data.paySign,
            success(res) {
              $wx.showToast('支付成功');

              setTimeout(callBack => {
                wx.navigateTo({
                  url: '/pages/other-pages/order-detail/order-detail?order_id=' + data.order_id,
                  success() {
                    _this.setData({
                      isPayment: false
                    });
                    _this.clearData();
                  }
                });
              }, 1500);
            },
            fail(res) {
              if (data.code == 202) {
                wx.showModal({
                  content: msg,
                  showCancel: false,
                  success(res) {
                    if (res.confirm) {
                      _this.setData({
                        isPayment: false
                      });
                      _this.getOrderList();
                    }
                  }
                })
              } else {
                $wx.showToast('支付失败');
              }
            }
          });
        }
      } else {
        $wx.showToast(msg);
      }
    });
  },
  
  // 删除订单
  delOrder(e){
    let _this = this, td = _this.data,
        order_id = e.currentTarget.dataset.id;
    
    wx.showModal({
      title: '提示',
      content: '确定要删除订单',
      success(res){
        if(res.confirm){
          wx.showLoading({
            title: '删除中...',
            mask: true
          });
          
          AJAX.delOrder({
            order_id: order_id
          }, (status, data, msg) => {
            wx.hideLoading();
            if(status){
              _this.getOrderList();
            }else{
              $wx.showToast(msg);
            }
          });
        }
      }
    })
  },

  /**
   * 订单列表
   * @param is_comment 1为没有评论
   * @param p 页数
   * @param num 每页请求条数
   * @param status 状态值 60为退款
   * */ 
  getOrderList(isPullDown){
    let _this = this, td = _this.data;
    wx.showNavigationBarLoading();
    
    AJAX.getOrderList(td.orderListParams ,(status, data, msg)=>{
      wx.hideNavigationBarLoading();
      
      if(status){
        if (isPullDown && td.orderList.data && data.data.length > 0){
          data.data = data.data.concat(td.orderList.data);
        }
        _this.setData({
          orderList: data
        });
      }else{
        // $wx.showToast(msg);
      }
    });
  },

  /*用户余额*/
  getMyMoney(params = {}){
    let _this = this;
    AJAX.getMyMoney(params, (status, data, msg)=>{
      if(status){
        _this.setData({
          myMoney: data.money
        });
      }else{
        $wx.showToast(msg);
      }
    });
  },

  /*清除数据*/ 
  clearData(){
    let td = this.data;
    td.orderListParams.page = 1;
  }
});